package com.ttl.common.db;

import hibernate.util.HibernateUtil;


import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;


public abstract class ADAO implements IDAO {

	protected SessionFactory sessionFactory;
	@Override
	public boolean add(Object o) {
		Session session = null;
    
		try {
			sessionFactory = HibernateUtil.getSessionFactory();
			session = sessionFactory.openSession();
			session.beginTransaction();
			//session.persist(o);
			session.save(o);
			session.getTransaction().commit();
			//sessionFactory.getCurrentSession().close();
			return true;
		}catch(Exception e) {
			System.out.println(e.toString());
			return false;
		}finally {
			session.close();
		}
	}

	@Override
	public void delete(String s) {
		Session session = null;

		try {
			SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
			session = sessionFactory.openSession();
			
			session.beginTransaction();
			
			Query q = session.createQuery(s);
			@SuppressWarnings("unused")
			int row = q.executeUpdate();
			
			
			session.getTransaction().commit();						
						
		}catch(Exception e) {
			System.out.println(e.toString());			
		}finally {
			session.close();
		}
	}
	
	@SuppressWarnings("rawtypes")
	public List find(String hql) {
		List result = null;
		Session session = null;

		try {
			SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
			session = sessionFactory.openSession();
			
			session.beginTransaction();
			
			Query q = session.createQuery(hql);
			result = q.list();
			
			session.getTransaction().commit();						
			
		}catch(Exception e) {			
			System.out.println(e.toString());
		}finally {
			session.close();
		}
		return result;
	}

	@SuppressWarnings("rawtypes")
	@Override
	public List findAll() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean update(String s, Object o) {
		return false;

	}

}
